<template>
    <div class="app-container">

      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色列表区域 -->
      <el-table :data="rolelist" border fit>

        <!-- 展开列 -->
        <el-table-column type="expand">
            <template slot-scope="scope">
                <el-row v-for="(item1, i1) in scope.row.children" :key="item1.id">
                    <el-col :span="5" >
                        <el-tag closable @close="removeRightById(scope.row, item1.authId)">{{item1.authName}}</el-tag>
                        <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="19" >
                        <!-- 渲染二级权限 6+18总共24列 -->
                        <el-row v-for="(item2, i2) in item1.children" :key="item2.id">
                            <el-col :span="6" >
                                <el-tag type="success" closable @close="removeRightById(scope.row, item2.authId)">{{item2.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <el-col :span="18">
                                 <!-- 渲染三级权限 -->
                                 <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable @close="removeRightById(scope.row, item3.authId)">{{item3.authName}}</el-tag> <!-- 这里的scope.row依然是整个数据 -->
                            </el-col>
                        </el-row>
                        
                    </el-col>
                </el-row>
                <span v-if="scope.row.children == 0">未分配权限</span>
            </template>
        </el-table-column>

        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting"  @click="showSetRightDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>

      </el-table>

        <!-- 分配权限的对话框 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" >

      <!-- 树形控件  -->
      <el-tree :data="rightslist" :props="treeProps" show-checkbox node-key="authId" default-expand-all :default-checked-keys="defKeys" ref="treeRef"></el-tree> 
      <!-- node-key="id"选中复选框时 是选中对应的id（？）
      这个id后面要用到 后面这个default-checked-keys就是默认选中的框 是根据id来选中的 
      后面还要把选中的id保存起来 都要用到这个id -->

      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>

    </el-dialog>
    </div>
</template>

<script>
    import {updateAuthMenu,getRolesList,deleteRoleAuth,getUserAuthChildrenList} from '@/api/role'
    export default {
        name:'role',
        data() {
            return {
            // 所有角色列表数据
            rolelist: [],
            // 控制分配权限对话框的显示与隐藏
            setRightDialogVisible: false,
            // 所有权限的数据
            rightslist: [],
            // 树形控件的属性绑定对象
            treeProps: {
                label: 'authName', /* 要展示哪个文本 */
                children: 'children' /* 子节点要取哪个东西 */
            },
            // 默认选中的节点Id值数组
            defKeys: [],
            // 当前即将分配权限的角色id
            currRoleId: ''
            }
        },
        created() {
          this.getRolesList()
        },
         methods: {
             allotRights(){
              // 把所有全选和半选的id保存到数组中
            //   this.$refs.treeRef 类似于dom.getelementByid("treeRef")
            //  let arr1 = this.$refs.treeRef.getCheckedKeys()
            //  let arr2 = this.$refs.treeRef.getHalfCheckedKeys()
            //  const idStr = [...arr1,...arr2]       ...ES6 的展开 数组或者对象
            //  arr2=[1,2,3]  ...arr2自动展开 [1,2,3]
               const keys = [
                    ...this.$refs.treeRef.getCheckedKeys(), /* getCheckedKeys是tree组件自带的方法 ...是展开到数组中 */
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ]
            //  const idStr = keys.join(',') /* 改为以逗号拼接的字符串 */
                updateAuthMenu(this.currRoleId,{authIds:keys}).then((response)=>{

                    this.$message.success('分配权限成功！')
                    this.getRolesList() /* 刷新 */
                    this.setRightDialogVisible = false /* 隐藏 */
                })

             },
             showSetRightDialog(row){
                this.currRoleId = row.roleId
                this.setRightDialogVisible = true
                getUserAuthChildrenList().then((response)=>{
                    this.rightslist = response.data
                    var Keys = []
                    row.children.forEach(item1 => {
                        // Keys.push(item1.authId)
                        item1.children.forEach(item2 => {
                            // Keys.push(item2.authId)
                            item2.children.forEach(item3 => {
                            Keys.push(item3.authId)
                        })
                        }) 
                    })
                    this.defKeys = Keys
                })
             },
            // 获取所有角色的列表
            getRolesList() {
                getRolesList().then((response)=>{
                    this.rolelist = response.data
                })
            },
            // 删除角色权限
            removeRightById(row,authId){
                this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(() => {
                        deleteRoleAuth(row.roleId,authId).then((response)=>{
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            row.children = response.data /* 返回的数据就是角色所拥有的新权限 */
                        })
                        
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });          
                    });
            },
        }
    }
</script>

<style lang="scss" scoped>

</style>